<?php

namespace app\api\controller;

use app\api\controller\Base;

/**
 * 首页接口
 */
class Index extends Base
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     * 
     */
    public function index()
    {
        //轮播图
        $banner = db('banner')->order('createtime desc')->select();
        //我的资产
        $data['gold'] = $this->user['gold']; //金币
        $data['silver'] = $this->user['silver']; //银币
        //我的旷工
        $where['status'] = ['in',[1,3]];
        $user_miners = db('user_miners')->field('id,miners_id,type,status,buy_time,end_time')->where(['user_id'=>$this->user['id']])->where($where)->select();
        foreach($user_miners as $k=>$v){
            $miners = db('miners')->where(['id'=>$v['miners_id']])->find();
            $user_miners[$k]['image'] = $miners['image']; //图片
            $user_miners[$k]['name'] = $miners['name']; //矿机 名称
            $user_miners[$k]['ability'] = ceil($miners['production_jin']/$miners['length']); //矿机 产币能力
            //矿机剩余天数
            if($v['status']==1){
                //计算天数
                $user_miners[$k]['days'] = '剩余使用期限'.floor(($v['end_time']-time())/(60*60*24)).'天';
            }
            if($v['status']==3){
                $user_miners[$k]['days'] = '永久';
            }
        }
        $data['banner'] = $banner;
        $data['user_miners'] = $user_miners;
        $this->success('操作成功',$data);
    }
    /**
     * 客服信息列表
     */
    public function msglist(){
        $where['user_id'] = $this->user['id'];
        //总数
        $count = db('service')->where($where)->count();
        //页
        $page = input('page')?input('page'):1;
        //一页显示数量
        $limit = input('limit')?input('limit'):10;
        $list = db('service')->where($where)->page($page,$limit)->select();
        foreach($list as $k=>$v){
            $list[$k]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
            $list[$k]['avatar'] = $this->user['avatar'];
            $list[$k]['username'] = $this->user['username'];
        }
        $data['list'] = $list;
        $data['count'] = $count;
        $this->success('操作成功',$data);
    }
    /**
     * 反馈
     */
    public function feedback(){
        //图片
        $images = input('images');
        //内容
        $content = input('content');
        if(empty($images) && empty($content)){
            $this->error('反馈信息不能空');
        }
        $data['user_id'] = $this->user['id'];
        $data['images'] = $images;
        $data['contents'] = $content;
        $data['create_time'] = time();
        $r = db('service')->insert($data);
        if($r){
            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }
    }
 
}
